package com.haiheng.videodoorbell.widget.webview

import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.view.View
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient
import com.tencent.smtt.export.external.interfaces.JsResult
import com.tencent.smtt.export.external.interfaces.PermissionRequest
import com.tencent.smtt.sdk.ValueCallback
import com.tencent.smtt.sdk.WebChromeClient
import com.tencent.smtt.sdk.WebView


/**
 *
 * @Des:
 * @Author: hwz
 * @Date:2022/3/25 0025
 * @Version: 1.0
 *  * - 播放网络视频配置
 * - 上传图片(兼容)
 */
class MyX5WebChromeClient constructor(ix5WebPageView: IX5WebPageView) : WebChromeClient() {
    var mIWebPageView: IX5WebPageView?= null
    init {
        this.mIWebPageView = ix5WebPageView
    }

    var FILECHOOSER_RESULTCODE = 1

    var FILECHOOSER_RESULTCODE_FOR_ANDROID_5 = 2

    private var mUploadMessage: ValueCallback<Uri>? = null

    private var mUploadMessageForAndroid5: ValueCallback<Array<Uri>>? = null

    private val mXProgressVideo: View? = null

    private var mXCustomView: View? = null

    private var mXCustomViewCallback: IX5WebChromeClient.CustomViewCallback? = null


    override fun onHideCustomView() {
        // 不是全屏播放状态

        // 不是全屏播放状态
        if (mXCustomView == null) {
            return
        }
//        mIWebPageView.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)

        mXCustomView!!.visibility = View.GONE
//        if (mIWebPageView.getVideoFullView() != null) {
//            mIWebPageView.getVideoFullView().removeView(mXCustomView)
//        }
        mXCustomView = null
//        mIWebPageView.hideVideoFullView()
        mXCustomViewCallback!!.onCustomViewHidden()
        mIWebPageView!!.showWebView()
    }
    /**
     * 播放网络视频时全屏会被调用的方法
     */
    override fun onShowCustomView(view: View, callback: IX5WebChromeClient.CustomViewCallback) {

//        mIWebPageView.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
        mIWebPageView?.hindWebView()
        // 如果一个视图已经存在，那么立刻终止并新建一个
        if (mXCustomView != null) {
            callback.onCustomViewHidden()
            return
        }

//        mIWebPageView.fullViewAddView(view)
        mXCustomView = view
        mXCustomViewCallback = callback
    }

    override fun onProgressChanged(p0: WebView?, newProgress: Int) {
        super.onProgressChanged(p0, newProgress)
        mIWebPageView!!.startProgress(newProgress)
    }

    override fun onReceivedTitle(p0: WebView?, p1: String?) {
        super.onReceivedTitle(p0, p1)

    }

    override fun getVideoLoadingProgressView(): View {
        return super.getVideoLoadingProgressView()

    }

    override fun getDefaultVideoPoster(): Bitmap {
        return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    }

    override fun openFileChooser(
        uploadMsg: ValueCallback<Uri>,
        acceptType: String?,
        capture: String?
    ) {
        openFileChooserImpl(uploadMsg)
    }

    // For Android > 5.0
    override fun onShowFileChooser(
        webView: WebView?,
        uploadMsg: ValueCallback<Array<Uri>>,
        fileChooserParams: FileChooserParams?
    ): Boolean {
        openFileChooserImplForAndroid5(uploadMsg)
        return true
    }

    private fun openFileChooserImplForAndroid5(uploadMsg: ValueCallback<Array<Uri>>) {
        mUploadMessageForAndroid5 = uploadMsg
        val contentSelectionIntent = Intent(Intent.ACTION_GET_CONTENT)
        contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE)
        contentSelectionIntent.type = "image/*"
        val chooserIntent = Intent(Intent.ACTION_CHOOSER)
        chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent)
        chooserIntent.putExtra(Intent.EXTRA_TITLE, "图片选择")
        mIWebPageView?.startFileChooserForResult(
            chooserIntent,
            FILECHOOSER_RESULTCODE_FOR_ANDROID_5
        )
    }

    override fun onJsAlert(p0: WebView?, p1: String?, p2: String?, p3: JsResult?): Boolean {
        p3?.confirm();
        return true;
//        return super.onJsAlert(p0, p1, p2, p3)

    }
    override fun onPermissionRequest(request: PermissionRequest?) {
        super.onPermissionRequest(request)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            request?.grant(request.getResources());
        }
    }
    private fun openFileChooserImpl(uploadMsg: ValueCallback<Uri>) {
        mUploadMessage = uploadMsg
        val intent = Intent(Intent.ACTION_GET_CONTENT)
        intent.addCategory(Intent.CATEGORY_OPENABLE)
        intent.type = "image/*"
        mIWebPageView?.startFileChooserForResult(
            Intent.createChooser(intent, "文件选择"),
            FILECHOOSER_RESULTCODE
        )
    }
}